﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

#if SILVERLIGHT
using System.Windows.Browser;
#else
using System.Configuration;
#endif

namespace QUT.Bio.Util {
	/// <summary>
	/// Portable run-time configuration.
	/// <para>The Silverlight implementation extracts settings from the HtmlElement's PARAM children and the Html page query string.</para>
	/// <para>The Wpf implementation extracts settings from app.config and the command line arguments</para>
	/// <para>In both cases, the command line overrides config settings if a setting appears in both places.</para>
	/// </summary>

	public static class Configuration {
		/// <summary>
		/// Keep a copy of the application configuration in a private dictionary.
		/// </summary>

		private static Arguments args = new Arguments();

#if SILVERLIGHT

		/// <summary>
		///	Initialise the configuration module by loading all parameters from the Silverlight plugin 
		///	and the Html query string.
		/// </summary>
		
		static Configuration () {
			try {
				foreach( HtmlElement childElement in HtmlPage.Plugin.Children ) {
					if ( childElement.TagName.Equals( "param", StringComparison.InvariantCultureIgnoreCase ) ) {
						args[ childElement.GetAttribute( "name" ) ] = childElement.GetAttribute( "value" );
					}
				}
				
				foreach ( var s in HtmlPage.Document.QueryString ) {
					args[s.Key] = s.Value;
				}
			}
			catch ( Exception e ) {
				HtmlPage.Window.Alert( String.Format( "{0}\n\n{1}\n\nDetails:\n{2}", 
					"An error occurred while loading parameters from the Html page.",
					"The application may not run properly as a result.",
					e 
				) );
			}
		}
#else
		/// <summary>
		///	Initialise the configuration module by loading all parameters from the Silverlight plugin 
		///	and the Html query string.
		/// </summary>

		static Configuration () {
			try {
				foreach ( var x in ConfigurationManager.AppSettings ) {
					if ( x is string ) {
						string key = (string) x;
						
						args.Add( key, ConfigurationManager.AppSettings[key] );
					}
				}
				
				args.Load( Environment.GetCommandLineArgs() );
			}
			catch ( Exception e ) {
				System.Windows.MessageBox.Show( String.Format( "{0}\n\n{1}\n\nDetails:\n{2}",
					"An error occurred while loading parameters from the Html page.",
					"The application may not run properly as a result.",
					e
				) );
			}
		}
#endif

		#region Indexer: string --> string
		/// <summary>
		/// Get an arguments object contining the merged config and command line values.
		/// </summary>
		/// <returns>The argument value.</returns>

		public static Arguments Args {
			get {
				return args;
			}
		}
		#endregion
	}
}
